package com.agh.portal.domain;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.EntityManager;
import javax.persistence.Enumerated;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.TypedQuery;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

import com.agh.portal.domain.enumeration.Gender;
import com.agh.portal.domain.enumeration.StudentStatus;

@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findStudentsByNameAndSurnameLike", "findStudentsByCityOfOrigin" })
public class Student {

    @NotNull
    @Size(min = 1, max = 30)
    private String name;

    @NotNull
    @Size(min = 1, max = 30)
    private String surname;

    @NotNull
    @Email
    private String email;

    @Enumerated
    private StudentStatus studentStatus;

    @Enumerated
    private Gender gender;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(style = "M-")
    private Date dateOfBirth;

    @Size(max = 60)
    private String cityOfOrigin;
    
    private String avatarId;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
    private Set<Question> questions = new HashSet<Question>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
    private Set<Answer> answers = new HashSet<Answer>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
    private Set<MainPagePost> mainPagePosts = new HashSet<MainPagePost>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
    private Set<Ad> ads = new HashSet<Ad>();

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "student")
    private Set<CourseStudent> courses = new HashSet<CourseStudent>();
    
    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "students")
    private Set<AghGroup> aghGroups = new HashSet<AghGroup>();

    public String toString() {
        return "Student [name=" + name + ", surname=" + surname + ", email=" + email + ", dateOfBirth=" + dateOfBirth + ", cityOfOrigin=" + cityOfOrigin + ", studentStatus=" + studentStatus + "]";
    }

    public static Long countUsersByEmailAddress(String emailAddress) {
        if (emailAddress == null || emailAddress.length() == 0) throw new IllegalArgumentException("The email argument is required");
        EntityManager em = Student.entityManager();
        TypedQuery q = em.createQuery("SELECT COUNT(o) FROM Student AS o WHERE o.email = :email", Long.class);
        q.setParameter("email", emailAddress);
        return ((Long) q.getSingleResult());
    }
    
    public static TypedQuery<Student> findStudentsByEmailAddress(String email) {
        if (email == null || email.length() == 0) throw new IllegalArgumentException("The email argument is required");
        EntityManager em = Student.entityManager();
        TypedQuery<Student> q = em.createQuery("SELECT o FROM Student AS o WHERE o.email = :email", Student.class);
        q.setParameter("email", email);
        return q;
    }
    
    @Transient
    Boolean locked;

}
